package com.nannybear.View;

import android.graphics.Color;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.jjoe64.graphview.series.DataPoint;
import com.nannybear.R;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

/**
 * \brief The humidity screen
 * \author Audrey LOISEAU
 * \date 16-03-2015
 */

/********************************* COPYRIGHT *********************************
 * Copyright (c) 2015, Audrey LOISEAU and Benjamin LANCINOT.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *************************************************************************************/

public class HumidityScreen extends Screen implements Observer {
	/********** DECLARATIONS **********/
    // Class
    private GraphViewClass graphViewClass;

    // Widget of the view
    private Button reset_button;
    private TextView hum_value, hum_average, warning_excl_hum, warning_text_hum, hum_DataPoint;
    private ImageView warningColor;
    // Handler
    private final Handler myHandler = new Handler();

    /************ METHODS ************/
    /**
     * \brief Constructor
     */
    public HumidityScreen(){
        super(R.layout.humidity_screen,false,true);
        graphViewClass = new GraphViewClass(false);
    }

    /**
     * \brief Update information with new value
     * \param obs   The observable class
     * \param obj   The object received
     */
    public void update(Observable obs, Object obj){
        String name = obs.getClass().getSimpleName();
        if(name.equalsIgnoreCase("Graphic")){
            updateHumidity((ArrayList< DataPoint>) obj);
            this.getGraphViewClass().update((ArrayList<DataPoint>) obj);
        }
        if(name.equalsIgnoreCase("AverageHumidity")){
            updateAverageHumidity(obj.toString());
        }
    }

    /**
     * \brief Handler to change the average humidity value on the textView
     * \param valueAverageHum   The average humidity value
     */
    void updateAverageHumidity(final String valueAverageHum) {
        myHandler.post(new Runnable() {
            @Override
            public void run() {
                hum_average.setText(valueAverageHum);
            }
        });
    }

    /**
     * \brief Handler to change the humidity value on the textView
     * \param mArrayList    The humidity array list
     */
    void updateHumidity(final ArrayList< DataPoint> mArrayList) {
        final double temp = mArrayList.get(mArrayList.size() -1).getY();
        myHandler.post(new Runnable() {
            @Override
            public void run() {if(temp != -50) {
                hum_value.setText(String.format("%.1f",temp));
            }
            else{
                hum_value.setText("xx");
                hum_average.setText("xx");
            }

            }
        });
    }

    /**
     * \brief Warn the user of a warning
     * \param warning    The warning value
     */
    public void changeToWarn(String warning){
        int posY, colorTxt;
        ViewGroup.LayoutParams params = warningColor.getLayoutParams();
        ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) warningColor.getLayoutParams();

        if ( warning.equals(" too dry")) {
            posY =185;
            params.height = 20;
            colorTxt= Color.parseColor("#5AAFED");
        }else if ( warning.equals(" too wet")){
            posY =105;
            params.height = 100;
            colorTxt= Color.parseColor("#B5262F");
        }else{
            posY =154;
            params.height = 50;
            colorTxt= Color.parseColor("#D3CDCD");
        }

        warningColor.setLayoutParams(params);
        p.setMargins(105, posY, 0, 0);
        warningColor.requestLayout();
        warning_text_hum.setText(warning);
        warning_text_hum.setTextColor(colorTxt);
    }

    /**
     * \brief Display the value of the point selected
     * \param valueDataPoint    The text to displayed
     */
    public void displayDatapoint(final String valueDataPoint) {
        myHandler.removeCallbacksAndMessages(null);
        myHandler.post(new Runnable() {
            @Override
            public void run() {
                hum_DataPoint.setVisibility(View.VISIBLE);
                hum_DataPoint.setText(valueDataPoint);
            }
        });

        myHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                hum_DataPoint.setVisibility(View.GONE);
            }
        }, 2000);
    }

    /**
     * \brief Update the view
     * \param humAverage    The humidity average
     * \param humValue  The humidity value
     * \param warning   The warning value
     */
    public void updateView(double humAverage, double humValue, String warning){
        hum_average.setText(String.format("%.1f",humAverage));
        hum_value.setText(String.format("%.1f",humValue));

        if (Double.parseDouble(warning) == 1) {
            warning = " perfect";
        }
        else if (Double.parseDouble(warning) == 2 || Double.parseDouble(warning) == 4) {
            warning = " too dry";
        }
        else{
            warning = " too wet";
        }
        changeToWarn(warning);
    }

    /**
     * \brief Show the view
     * \param thresholdTemp The value of temperature threshold
     * \param thresholdHum  The value of humidity threshold
     */
    public void display(int thresholdTemp, int thresholdHum){
        getTempButton().setBackgroundResource(getTempDrawable()[thresholdTemp]);
        getHumidityButton().setBackgroundResource(getHumDrawable()[thresholdHum]);
    }

    /**
     * \brief Close the view (empty)
     */
    public void close(){
    }

    /*********** ACCESSORS ***********/
    /**
     * \brief Get method for the graphView class
     */
    public GraphViewClass getGraphViewClass() {
        return graphViewClass;
    }

    /**
     * \brief Set method for the humidity value
     */
    public void setHum_value(TextView hum_value) {
        this.hum_value = hum_value;
    }

    /**
     * \brief Set method for the humidity average
     */
    public void setHum_average(TextView hum_average) {
        this.hum_average = hum_average;
    }

    /**
     * \brief Set method for the reset button
     */
    public void setReset_button(Button reset_button) {
        this.reset_button = reset_button;
    }

    /**
     * \brief Set method for the warning
     */
    public void setWarning_text_hum(TextView warning_text_hum) {
        this.warning_text_hum = warning_text_hum;
    }

    /**
     * \brief Set method for the warning color
     */
    public void setWarningColor(ImageView warningColor) {
        this.warningColor = warningColor;
    }

    /**
     * \brief Set method for the humidity indicator
     */
    public void setHum_DataPoint(TextView hum_DataPoint) {
        this.hum_DataPoint = hum_DataPoint;
    }
}
